//
//  YQSQLiteTool.m
//  industry
//
//  Created by 王雅强 on 2021/2/2.
//  Copyright © 2021 Haier. All rights reserved.
//

#import "YQSQLiteTool.h"
#import <FMDB/FMDB.h>

@interface YQSQLiteTool()

@property (nonatomic, strong) FMDatabaseQueue * dbQueue;

@end

@implementation YQSQLiteTool

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [XLNotif addObserver:self selector:@selector(userLoginSuccess) name:XLNotificationUserLoginSuccess object:nil];
    }
    return self;
}

-(void)userLoginSuccess{
    self.dbQueue = [[FMDatabaseQueue alloc] initWithPath:[NSString stringWithFormat:@"%@/db.sqlite",[self createDir]]];
}

#pragma mark 使用NSSearchPathForDirectoriesInDomains创建文件目录
- (NSString *)createDir {
    


    NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *dataFilePath = [docsdir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",XL_UserInfoData.userId]];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL isDir = NO;

    // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
    BOOL existed = [fileManager fileExistsAtPath:dataFilePath isDirectory:&isDir];

    if (!(isDir && existed)) {
        // 在Document目录下创建一个archiver目录
        [fileManager createDirectoryAtPath:dataFilePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    XLLog(@"新路径信息%@",dataFilePath);
    return dataFilePath;
    
}

/// 建表
-(void)createTable{
//    "create table if not exists t_stu(id integer primary key autoincrement, name text not null, age integer, score real default 60)"
    
    [self.dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
            
    }];
}

/// 批量处理
-(void)executeStaments{
    [self.dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
//        "insert into t_stu(name, age, score) values('zhangsan', 15, 88);insert into t_stu(name, age, score) values('zhangsan2', 152, 881)"
    }];
}

/// 修改
-(void)updateRecord{
    [self.dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
            
    }];
}

/// 删除
-(void)deleteRecord{
    [self.dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
            
    }];
}

/// 插入
-(void)insertRecord{
    [self.dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
//        "insert into t_stu(name, age, score) values('zhangsan', 15, 88)"
    }];
}


/// 查询
-(void)queryAll{
    [self.dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
//        "select * from t_stu"
    }];
}





@end
